package com.itheima.wisdomnext.mapper;

import com.itheima.wisdomnext.entity.Announcement;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AnnouncementMapper {

    // 查询所有公告
    @Select("SELECT * FROM announcements WHERE is_active != 0")
    List<Announcement> findAllAnnouncements();

    // 根据公告 ID 查询公告
    @Select("SELECT * FROM announcements WHERE id = #{id}  AND is_active != 0")
    Announcement findAnnouncementById(@Param("id") Integer id);

    // 插入公告
    @Insert("INSERT INTO announcements (title, content, administrator_id) " +
            "VALUES (#{title}, #{content}, #{administratorId})")
    void insertAnnouncement(Announcement announcement);

    // 更新公告
    @Update("UPDATE announcements SET title = #{title}, content = #{content}, " +
            "image_url = #{imageUrl}, administrator_id = #{administratorId}, is_active = #{isActive}, " +
            "updated_at = CURRENT_TIMESTAMP WHERE id = #{id}")
    void updateAnnouncement(Announcement announcement);

    // 修改公告的状态为失效（软删除）
    @Update("UPDATE announcements SET is_active = false WHERE id = #{id}")
    void deactivateAnnouncement(@Param("id") Integer id);
}
